#include "head.h"

typedef struct train_s{
    int len;
    char buf[1000];
}train_t;
//传输大文件
int do_work(int net_fd){
    printf("do_work_begin");
    char *file_name = "1b.txt";
    train_t tag;
    bzero(&tag, sizeof(tag));

    //打开文件   
    int file_fd = open(file_name, O_RDONLY);
    ERROR_CHECK(file_fd, -1, "open_error");
    //获取文件大小
    struct stat stat_file;
    int ret_fstat = fstat(file_fd, &stat_file);
    ERROR_CHECK(ret_fstat, -1, "fastat_error");                   
    //发送文件大小
    send(net_fd, &stat_file.st_size, sizeof(off_t), MSG_NOSIGNAL);

    //发送文件名
    tag.len = strlen(file_name);
    memcpy(tag.buf, file_name, tag.len);
    int ret_send1 = send(net_fd, &tag, sizeof(int) + tag.len, MSG_NOSIGNAL);
    ERROR_CHECK(ret_send1, -1, "send_file_name_error");

    while(1){
        //sleep(1);
        char buf[1000] = { 0 };
        int read_num = read(file_fd, buf, sizeof(buf));
        ERROR_CHECK(read_num, -1, "read_error");
        
        //判断是否读空
        if(read_num == 0){
            printf("读取文件完成!\n");
            break;
        }
        //sleep(0.01);
        int ret_send2 = send(net_fd, &read_num, sizeof(int), MSG_NOSIGNAL);
        ERROR_CHECK(ret_send2, -1, "send_content_size_error");
        //printf("1");
        if(ret_send2 == -1){
            printf("对方断开连接!\n");
            break;
        }  
        int ret_send3 = send(net_fd, buf, read_num, MSG_NOSIGNAL);
        ERROR_CHECK(ret_send3, -1, "send_content_error");
        //printf("2");
        if(ret_send3 == -1){
            printf("对方断开连接!\n");
            break;
        }
    }

    close(file_fd);
    return 0;
}


